package leetcode.Leetcode56;

import java.util.*;

public class DraftSolution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[0];
            }
        });
        List<int[]> merged = new ArrayList<>();
        for (int i = 0; i < intervals.length; i++) {
            int lNum = intervals[i][0];
            int rNum = intervals[i][1];
            if (merged.size() == 0 || merged.get(merged.size() - 1)[1] < lNum) {//无法合并的
                merged.add(new int[]{lNum, rNum});
            } else {
                merged.get(merged.size() - 1)[1] = Math.max(merged.get(merged.size() - 1)[1], rNum);
            }
        }
        return merged.toArray(new int[merged.size()][]);
    }

    public static void main(String[] args) {
        DraftSolution solution = new DraftSolution();
        solution.merge(new int[][]{{15,18},{1,3},{2,6},{8,10}});
    }
}


